package com.lsu.app.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsu.app.bean.Vocabulary;
import com.lsu.app.service.VocabularyService;
import com.lsu.app.mapper.VocabularyMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 *
 * @description 针对表【vocabulary(分词器频率词)】的数据库操作Service实现
 * @Date 2023-11-13 19:48:25
 * @author hjj
 */
@Service
public class VocabularyServiceImpl extends ServiceImpl<VocabularyMapper, Vocabulary> implements VocabularyService{
    private final VocabularyMapper mapper;

    public VocabularyServiceImpl(VocabularyMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public void insertData(Integer sendId,Integer courseId, Set<String> words) {
        for (String s : words) {
            Integer id = mapper.getIdByWord(s.substring(1),courseId);
            if (id==null){
                Integer type = Integer.getInteger(String.valueOf(s.charAt(0)));
                mapper.insert(new Vocabulary(courseId,s.substring(1),type));
                mapper.insertAllWithUV(sendId,mapper.getIdByWord(s.substring(1),courseId));
            }else{
                mapper.addFrById(id);
            }
        }
    }

    @Override
    public String[] getQuWordsByCourseId(Integer courseId) {
        return mapper.getQuWordsByCourseId(courseId).toArray(new String[0]);
    }

    @Override
    public Integer getCountByCourseIdAndType(Integer courseId, Integer type) {
        Integer sum  = mapper.getCountByCourseIdAndType(courseId, type);
        return sum==null?0:sum;
    }

    @Override
    public List<Integer> getUserIdByWord(String word,Integer courseId) {
        return mapper.getUserIdByWord(word,courseId);
    }

    @Override
    public List<Integer> getCourseIdByWord(String word) {
        return mapper.getCourseIdByWord(word);
    }

    @Override
    public List<Vocabulary> getVocasByCourseIds(Integer teacherId) {
        return mapper.getVocasByTeacherId(teacherId);
    }
}




